package com.bianmaba.examples.mybatis.service;

import com.bianmaba.examples.mybatis.MyBatisExampleApplication;
import com.bianmaba.examples.mybatis.bean.order.Order;
import com.bianmaba.examples.mybatis.mapper.OrderMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: bianmaba-web-framework-extends
 * @description:
 * @author: Chenjiabin
 * @create: 2018-04-22 20:29
 **/

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {MyBatisExampleApplication.class})
@Transactional()
public class UserServiceTester {
    @Autowired
    private OrderService service;
    private List<Order> orders = new ArrayList<>(0);

    @Before
    public void before() {
        for (long i = 0; i < 11; i++) {
            Order order = new Order();
            order.setNumber("order" + i);
            order.setTotal(i + 0d);
            long val = service.insert(order);
            orders.add(order);
        }
    }

    @Test
    public void insert() throws Exception {
        List<Order> orders1 = service.findAll();
        Order order = new Order();
        order.setNumber("aa");
        order.setTotal(11.d);
        long val = service.insert(order);
        Assert.assertEquals(order.getId() - orders.get(orders.size() - 1).getId(), 1);

        List<Order> orders2 = service.findAll();
        Assert.assertEquals(orders2.size() - orders1.size(), 1);
    }

    @Test
    public void testUserMapper() throws Exception {
        List<Order> orders = service.findAll();
        Assert.assertEquals(orders.size(), 11);


        orders = service.findAll(1, 5);
        Assert.assertEquals(orders.size(), 5);
        orders = service.findAll(3, 5);
        Assert.assertEquals(orders.size(), 1);


    }
}